//
//  OpenNativeModule.m
//  steamRoom
//
//  Created by 安建伟 on 2019/7/19.
//  Copyright © 2019 com.thingcom. All rights reserved.
//

#import "OpenNativeModule.h"
#import "MyDeviceViewController.h"

@implementation OpenNativeModule

- (instancetype)init {
    self = [super init];
    if (self) {
        NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
        [defaultCenter removeObserver:self];
        [defaultCenter addObserver:self
                          selector:@selector(sendCustomEvent:)
                              name:@"sendCustomEventNotification"
                            object:nil];
    }
    return self;
}

RCT_EXPORT_MODULE();

//无参
RCT_EXPORT_METHOD(openNativeVC) {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        MyDeviceViewController *vc = [[MyDeviceViewController alloc] init];
        [[UIViewController getCurrentVC] presentViewController:vc animated:YES completion:nil];
    });
}

//有参
RCT_EXPORT_METHOD(jump2Native:(NSDictionary *) dataByRN resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"rn传过来的参数为%@",dataByRN);
        NSDictionary *data = [dataByRN valueForKey:@"data"];
        NSString *value = [data valueForKey:@"value"];
        NSString *deviceName = [data valueForKey:@"deviceName"];
        DeviceDataPoint dataPoint = [[GizManage shareInstance] judgeDeviceNameWith:deviceName];
        [[GizManage shareInstance] writeDataPoint:dataPoint value:value];
        if (([[dataByRN objectForKey:@"data"] isKindOfClass:[NSDictionary class]] && [[dataByRN objectForKey:@"data"] count] > 0)) {
            resolve(@(YES));
        } else {
            reject(@"warning", @"data cannot be empty!", nil);
        }
        
    });
}

/// 接收通知的方法，接收到通知后发送事件到RN端。RN端接收到事件后可以进行相应的逻辑处理或界面跳转
- (void)sendCustomEvent:(NSNotification *)notification {
    NSDictionary *data = [notification userInfo];
    [self sendEventWithName:@"CustomEventName" body:data];
}

/// 重写方法，定义支持的事件集合
- (NSArray<NSString *> *)supportedEvents {
    return @[@"CustomEventName"];
}


@end
